Day 7 - Hangman


Posted by pei_______ on 2022-04-17

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


import random
from replit import clear
from hangman_art import logo , stages
from hangman_words import word_list

print(logo)
chosen_word = random.choice(word_list)
word_length = len(chosen_word)
already_guessed = []
lives = 6

display = []
for _ in range(word_length):
    display += "_"

while "_" in display and lives != 0:
  guess = input("Guess a letter: ").lower()
  clear()
  if guess in already_guessed:
    print(f"You've already guessed {guess}.")
  elif guess not in chosen_word:
    print(f"You guess {guess}, that's not in the word. You lose a life.")
    lives -= 1
  for position in range(word_length):
      letter = chosen_word[position]
      if letter == guess:
        display[position] = letter

  already_guessed += guess
  print(stages[lives])
  print(f"{' '.join(display)}")

if "_" not in display:
  print("\nYou win!")

if lives == 0:
  print("\nYou lose!")

#Python #課堂筆記 #100 Days of Code







Related Posts

Browser Rendering Optimization

Browser Rendering Optimization

何謂演算法技術面試?讀《Cracking the Coding Interview(提升程式設計師的面試力)》

何謂演算法技術面試?讀《Cracking the Coding Interview(提升程式設計師的面試力)》

Tally String Times with Reduce

Tally String Times with Reduce


Comments